//
//  FourthCollectionViewCell.m
//  NewMusic
//
//  Created by chuxinyu123 on 15/11/23.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "FourthCollectionViewCell.h"
#import "CustomerNetworking.h"
#import "FourthModel.h"
#import "FourthTableViewCell.h"

@interface FourthCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) FourthModel *fourthModel;
@property (nonatomic, assign) CGFloat numberHeight;
@property (nonatomic, assign) CGFloat contenfOffSetY;

@end
@implementation FourthCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.numberHeight = 0;
        [self createFourth];
    }
    return self;
}

- (void)customer
{
    NSString *string = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@?detail=true&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=700606&bundle_id=com.ttpod.music&latitude=38.88243&longtitude=121.54", _IDString];
    [CustomerNetworking customerConnectWithURL:string andParameters:nil andBlock:^(id response) {
        NSDictionary *dictionary = response;
        self.fourthModel = [FourthModel fourthModelWithDictionary:[dictionary objectForKey:@"data"]];
        CGFloat first = [FourthTableViewCell heightWithFont:[UIFont systemFontOfSize:17] andText:self.fourthModel.brief andWidth:self.tableView.frame.size.width - 30];
        CGFloat second = [FourthTableViewCell heightWithFont:[UIFont systemFontOfSize:17] andText:self.fourthModel.personalLife andWidth:self.tableView.frame.size.width - 30];
        CGFloat third = [FourthTableViewCell heightWithFont:[UIFont systemFontOfSize:17] andText:self.fourthModel.entertainmentExperience andWidth:self.tableView.frame.size.width - 30];
        CGFloat fourth = [FourthTableViewCell heightWithFont:[UIFont systemFontOfSize:17] andText:self.fourthModel.awards andWidth:self.tableView.frame.size.width - 30];
        self.numberHeight = 150 + first + second + third + fourth;
        [self.tableView reloadData];
    }];
    
}

- (void)setIDString:(NSString *)IDString
{
    if (_IDString != IDString) {
        [_IDString release];
        _IDString = [IDString retain];
        [self customer];
    }
}

- (void)createFourth
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.contenfOffSetY = _tableView.contentOffset.y;
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 250)];
    headView.alpha = 0;
    self.tableView.tableHeaderView = headView;
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableFooterView = footView;
    
    [self.tableView registerClass:[FourthTableViewCell class] forCellReuseIdentifier:@"FourthCell"];
    [self.contentView addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FourthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FourthCell" forIndexPath:indexPath];
    cell.fourthModel = _fourthModel;
    cell.backgroundColor = [UIColor clearColor];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _numberHeight;
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView.contentOffset.y < _contenfOffSetY) {
//        [self.delegate FourthTableViewScrollFrame:scrollView.contentOffset.y - _contenfOffSetY tableViewContentOffY:scrollView.contentOffset.y top:NO];
//    }else {
//        [self.delegate FourthTableViewScrollFrame:scrollView.contentOffset.y - _contenfOffSetY tableViewContentOffY:scrollView.contentOffset.y top:YES];
//    }
//    _contenfOffSetY = scrollView.contentOffset.y;
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
